package com.handle;

import com.constant.ChannelMap;
import com.register.RegisterInfo;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * @author linyu.dai
 * @project rpc
 * @description 心跳检测处理器
 * @creat 2022/11/29 09:53:30
 */
@Slf4j
public class IdleStateEventHandle extends ChannelDuplexHandler {

    //事件触发时 调用
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        IdleStateEvent event = (IdleStateEvent) evt;
        if(event.state() == IdleState.READER_IDLE){
            //剔除相关信息 连接信息
            String serverName = ChannelMap.removeChannelServer(ctx.channel());
            //剔除注册表信息
            RegisterInfo.remove(serverName);
            //通知服务不可用
            ChannelMap.notifyClient(serverName);
        }
        super.userEventTriggered(ctx, evt);
    }

}
